package com.ruoyi.application.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ShenShiHao
 */
public class PwdUtils {

    private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]+");
    private static final Pattern LOWERCASE_PATTERN = Pattern.compile("[a-z]+");
    private static final Pattern UPPERCASE_PATTERN = Pattern.compile("[A-Z]+");

    public static boolean checkPwdComplex(String password) {
        // 用于判断密码是否过于简单
        Matcher m = NUMBER_PATTERN.matcher(password);
        if (!m.find()) {
            return false;
        }
        m.reset().usePattern(LOWERCASE_PATTERN);
        if (!m.find()) {
            return false;
        }
        m.reset().usePattern(UPPERCASE_PATTERN);
        if (!m.find()) {
            return false;
        }
        return true;
    }

    /**
     * 密码复杂度的确认
     * 大小写字母、数字及特殊符号等混合组成
     */
    public static boolean checkPwdComplexMore(String pwd) {
        //大小写字母、数字及特殊符号等混合组成
        if (pwd.matches(".*[a-z]{1,}.*") &&
                pwd.matches(".*[A-Z]{1,}.*") &&
                pwd.matches(".*\\d{1,}.*") &&
                pwd.matches(".*[~`!@#$%^&*()-_=+,<.>/?'\";:|{}\\[\\]\\\\]{1,}.*")) {
            String[] dict = new String[]{"password", "admin", "root",
                    "012", "123", "234", "345", "456", "567", "678", "789", "987",
                    "876", "765", "654", "543", "432", "321", "210"};
            //判断不包含连续数字,以及保留字,用的穷举
            for (String s : dict) {
                if (pwd.contains(s)) {
                    return false;
                }
            }
            return checkContinuity(pwd.toLowerCase());
        }
        return false;
    }

    /**
     * 判断键盘不连续
     */
    private static boolean checkContinuity(String password) {
        char[][] keyCode = {
                {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'},
                {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'},
                {'z', 'x', 'c', 'v', 'b', 'n', 'm'}
        };

        char[] c = password.toCharArray();
        List<Integer> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        for (char temp : c) {
            toHere:
            for (int j = 0; j < keyCode.length; j++) {
                for (int k = 0; k < keyCode[j].length; k++) {
                    if (temp == keyCode[j][k]) {
                        x.add(j);
                        y.add(k);
                        break toHere;
                    }
                }
            }
        }
        boolean flag = true;
        for (int i = 0; i < x.size() - 2; i++) {
            if (x.get(i).equals(x.get(i + 1)) && x.get(i + 1).equals(x.get(i + 2))) {
                //三者在同一y行上
                if (y.get(i) > y.get(i + 2)) {
                    if (y.get(i) - 1 == y.get(i + 1) && y.get(i) - 2 == y.get(i + 2)) {
                        flag = false;
                        break;
                    }
                } else {
                    if (y.get(i) + 1 == y.get(i + 1) && y.get(i) + 2 == y.get(i + 2)) {
                        flag = false;
                        break;
                    }
                }
            } else if (!x.get(i).equals(x.get(i + 1)) && !x.get(i + 1).equals(x.get(i + 2)) && !x.get(i).equals(x.get(i + 2))) {
                //三者均不在同一行上
                if (x.get(i) > x.get(i + 2)) {
                    if ((x.get(i) - 1 == x.get(i + 1) && x.get(i) - 2 == x.get(i + 2))) {
                        flag = false;
                    }
                } else {
                    if (x.get(i) + 1 == x.get(i + 1) && x.get(i) + 2 == x.get(i + 2)) {
                        flag = false;
                        break;
                    }
                }
            }
        }
        return flag;
    }

}
